package com.seeyon.apps.ssoAndDoc.utils;

import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.web.util.WebUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

public class TransformationUtils {
	/** 日志管理器 */
    private static final Log LOGGER = LogFactory.getLog(TransformationUtils.class);
    
    /**
     * 将html文单转换为PDF文单</br>
     * 需要服务器上安装PDF转换工具，并且在插件配置参数中配置转换工具路径
     * @param htmlFilePath 原始html文件绝对路径
     * @param pdfFilePath 转换后pdf文件绝对路径
     */
    public static boolean transHtmlToPdf(String htmlFilePath, String pdfFilePath) {
        String wkhtmltopdfToolPath = AppContext.getSystemProperty("gcdBatch.wkhtmltopdf");

        try {
            String cookieName = "JSESSIONID";
            String cookieValue = null;
            HttpServletRequest request =WebUtil.getRequest();
            Cookie[] cookies = request.getCookies();
            for (Cookie ck : cookies) {
                if(cookieName.equals(ck.getName().toUpperCase())){
                    cookieValue = ck.getValue();
                }
            }
            // html 转 pdf 工具 不同操作系统运行不同的命令
            String command = "";
            String wkhtmltopdf = "wkhtmltopdf";
            if(System.getProperty("os.name").startsWith("Windows")) {
                if(StringUtils.isNotBlank(wkhtmltopdfToolPath)) {
                    command = wkhtmltopdfToolPath + "  --cookie "+cookieName+" "+cookieValue+" "+" --javascript-delay 1500 --no-stop-slow-scripts \"" + htmlFilePath + "\" " + pdfFilePath;
                }
            } else {
                //pzh环境为信创
                command = wkhtmltopdf+ "  --cookie "+cookieName+" "+cookieValue+" "+" --javascript-delay 1500 --no-stop-slow-scripts \"" + htmlFilePath + "\" " + pdfFilePath;
            }
            LOGGER.info("转化PDF命令:"+command);
            Process process = Runtime.getRuntime().exec(command);
            new StreamGobbler(process.getInputStream(), "input").start();
            new StreamGobbler(process.getErrorStream(), "error").start();
            if(process.waitFor() == 0) {
                LOGGER.info("转PDF成功:" + pdfFilePath);
                return true;
            }
        } catch(Exception e) {
            LOGGER.info("文单转PDF异常:" + e.getMessage());
        }
        return false;
    }

    /**
     * 将html文单转换为PDF文单
     * @param htmlFilePath 免登陆controller地址
     * @param pdfFilePath 原始html文件路径
     * @param cookies
     * @return
     */
    public static boolean transUrlToPdf(String htmlFilePath, String pdfFilePath,Cookie[] cookies) {
        String wkhtmltopdfToolPath = "H:\\tempFile\\wkhtmltopdf\\bin\\wkhtmltopdf.exe";

        try {
            String cookieName = "JSESSIONID";
            String cookieValue = null;
            for (Cookie ck : cookies) {
                if(cookieName.equals(ck.getName().toUpperCase())){
                    cookieValue = ck.getValue();
                }
            }
            // html 转 pdf 工具 不同操作系统运行不同的命令
            String command = "";
            String wkhtmltopdf = "wkhtmltopdf";
            if(System.getProperty("os.name").startsWith("Windows")) {
                if(StringUtils.isNotBlank(wkhtmltopdfToolPath)) {
                    command = wkhtmltopdfToolPath +" --javascript-delay 1000 \"" + htmlFilePath + "\" " + pdfFilePath;
                }else{
                    return false;
                }
            } else {
                //pzh环境为信创
                command = wkhtmltopdf + " --javascript-delay 1000 \"" + htmlFilePath + "\" " + pdfFilePath;
            }
            LOGGER.info("转化PDF命令: "+command);
            Process process = Runtime.getRuntime().exec(command);
            new StreamGobbler(process.getInputStream(), "input").start();
            new StreamGobbler(process.getErrorStream(), "error").start();
            if(process.waitFor() == 0) {
                LOGGER.info("转PDF成功: " + pdfFilePath);
                return true;
            }

        } catch(Exception e) {
            LOGGER.info("文单转PDF异常: " + e.getMessage());
        }
        return false;
    }

}
